<template>
  <div class="header">
    <div class="left">
      <div class="tabs">
        <div
          class="tab"
          v-for="(item, index) in tabsParam"
          @click="tabsHandle(index)"
          :class="{ active: index === flag - 1 }"
          :key="index"
        >
          {{ item }}
        </div>
      </div>
      <div class="time">
        <p>
          已选时间：{{ props.timeRange[0] }} 至
          {{ props.timeRange.at(-1) }}
        </p>
      </div>
    </div>
    <el-button class="right" @click="exportHandle">数据导出</el-button>
  </div>
</template>
<script setup lang="ts">
import { getExport } from '@/api/admin/statistics'
import { ElMessageBox } from 'element-plus'
import { ref } from 'vue'
const props = defineProps<{
  timeRange: Array<String>
  flag: number
}>()

const emit = defineEmits(['getDate'])
const tabsParam = ['昨日', '近7日', '近30日', '本周', '本月']
const flag = ref(props.flag)
// 时间切换
const tabsHandle = (index: number) => {
  flag.value = index + 1
  emit('getDate', index + 1)
}
// 数据导出
const exportHandle = () => {
  ElMessageBox.confirm('是否确认导出最近30天运营数据?', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    let res = await getExport()
    console.log('res.data的类型:', typeof res.data)
    console.log(res.data)
    let url = window.URL.createObjectURL(res.data)
    var a = document.createElement('a')
    document.body.appendChild(a)
    a.href = url
    a.download = '运营数据统计报表.xlsx'
    a.click()
    document.body.removeChild(a)
    window.URL.revokeObjectURL(url)
  })
}
</script>
<style scoped lang="scss">
.header {
  height: var(--main-height);
  line-height: 40px;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: space-between;
  padding-bottom: 20px;
  .left {
    display: flex;
    align-items: center;
    gap: 1px;
    .tabs {
      width: 400px;
      background-color: var(--main-background);
      display: flex;
      border: 1px solid #e5e4e4;
      border-radius: 5px;
      .tab {
        font-size: 16px;
        cursor: pointer;
        color: #333;
        letter-spacing: -0.2px; // 文字之间的间距
        flex: 1;
        transition:
          font-size 0.2s,
          font-weight 0.2s,
          background-color 0.2s;
      }
      .active {
        font-weight: 700;
        background: #ffd04b;
      }
    }

    .time {
      margin-left: 10px;
      font-size: 14px;
      color: #666;
    }
  }
  .right {
    margin-left: auto;
  }
}
</style>
